package org.ucn.dmaa0914.temadesign.group2.tuiLayer;

import java.util.ArrayList;
import java.util.Scanner;
import org.ucn.dmaa0914.temadesign.group2.StringUtil;

/**
 * Display is the base class for all tui-pages, including menus, editors etc.
 * 
 * @author Kim Johannsen
 * @version 05-11-2014
 */
public class Display
{
    private static final int consoleWidth = 130;
    private static final int consoleHeight = 40;
    private static final int resultTextHeight = 20;
    
    private static Display currentDisplay;
    
    private String title;
    private String content;
    private String inputLabel;
    private String lastInput;
    
    public Display(String title, String inputLabel)
    {
        this.title = title;
        this.content = "";
        this.lastInput = "";
        this.inputLabel = inputLabel;
    }
    
    public String execute()
    {       
        // Flush all previous content
        for( int i = 0; i < 20; ++i )
        {
            System.out.println("\n");
        }
        
        System.out.println(StringUtil.repeatString("=", consoleWidth));
        
        int amountSpaces = consoleWidth - 2 - title.length();
        int amountSpacesLeft = amountSpaces / 2;
        int amountSpacesRight = amountSpaces - amountSpacesLeft;        
        
        // title area
        System.out.println(
            "|" +
            StringUtil.repeatString(" ", amountSpacesLeft) +
            title +
            StringUtil.repeatString(" ", amountSpacesRight) +
            "|");
            
        System.out.println(StringUtil.repeatString("=", consoleWidth));
        // Status bar
        System.out.println("|" + Application.instance.makeStatusBar(consoleWidth - 2) + "|");
        System.out.println(StringUtil.repeatString("=", consoleWidth));
        
        // Result Text Area
        System.out.println(Application.instance.getResultText());
        int resultTextLines = Application.instance.getResultText().length() - Application.instance.getResultText().replace("\n", "").length() - 1;
        // Draw Newlines to fill result text area
        for( int i = 0; i < resultTextHeight - resultTextLines; ++i )
        {
            System.out.println("");
        }
        
        System.out.println(StringUtil.repeatString("=", consoleWidth));
        
        // content area
        System.out.println(content);
        int contentLines = content.length() - content.replace("\n", "").length() - 1;
        
        // Draw Newlines to fill content area
        for( int i = 0; i < (consoleHeight - contentLines - 8 - resultTextHeight); ++i )
        {
            System.out.println("");
        }
        
        // input area
        System.out.println(StringUtil.repeatString("=", consoleWidth));
        
        return prompt(inputLabel);
    }
    
    public String prompt(String label)
    {
        System.out.print(label);
        
        Scanner scanner = new Scanner(System.in);
        return scanner.nextLine();
    }
    
    public void setTitle(String title)
    {
        this.title = title;
    }
    
    public void setContent(String content)
    {
        this.content = content;
    }
    
    public void setInputLabel(String inputLabel)
    {
        this.inputLabel = inputLabel;
    }
    
    public String getTitle()
    {
        return title;
    }
    
    public String getContent()
    {
        return content;
    }
    
    public String getInputLabel()
    {
        return inputLabel;
    }
    
    public String getLastInput()
    {
        return lastInput;
    }
}
